package 笔试;

public class 字符串的左右移 {
    public String stringShift(String s, int[][] shift) {
        if(s == null || s.length() == 0) {
            return "";
        }
        int type = 0;
        for(int i = 0; i < shift.length; i++) {
            if(shift[i][0] == 0) {
                for(int j = 0; j < shift[i][1]; j++) {
                    type--;
                }
            } else {
                for(int j = 0; j < shift[i][1]; j++) {
                    type++;
                }
            }
        }
        char[] arr = s.toCharArray();
        if(type > 0) {
            for(int i = 0; i < type; i++) {
                reverseRightWords(arr);
            }
        } else {
            type = - type;
            for(int i = 0; i < type; i++) {
                reverseLeftWords(arr);
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    public void reverseLeftWords(char[] arr) {
        char c = arr[0];
        int i = 0;
        for(; i < arr.length-1; i++) {
            arr[i] = arr[i+1];
        }
        arr[i] = c;
    }
    public void reverseRightWords(char[] arr) {
        char c = arr[arr.length-1];
        int i = arr.length-1;
        for(;i > 0; i--) {
            arr[i] = arr[i-1];
        }
        arr[i] = c;
    }
}
